/**
 * 
 */
package org.ycsoft.metroid;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

/**
 * @author Unilog
 *
 */
public class SearchFields extends Dialog {
	
	private AutoCompleteTextView txtDep, txtArr;
	private String departure;
	private String arrival;
	private Button btnCompute;
	private Button btnCancel;
	
	private String[] stops;

	private View.OnClickListener okListener;
	
	public SearchFields(Context context){
		super(context);
	}
	
	@Override
	public void onStart(){
		
		setContentView(R.layout.popup);
		this.setTitle(R.string.enter_stops);
		
		txtDep = (AutoCompleteTextView)findViewById(R.id.txtDeparture);
		txtArr = (AutoCompleteTextView)findViewById(R.id.txtArrival);
		btnCompute = (Button)findViewById(R.id.btnCompute);
		btnCancel = (Button)findViewById(R.id.btnCancel);
		
		if(departure != null)
			txtDep.setText(departure);
		if(arrival != null)
			txtArr.setText(arrival);
		
		if(stops != null){
			txtDep.setAdapter(new ArrayAdapter<String>(this.getContext(),R.layout.list,stops));
			txtArr.setAdapter(new ArrayAdapter<String>(this.getContext(),R.layout.list,stops));
		}
		
		btnCompute.setOnClickListener(searchListener);
		btnCancel.setOnClickListener(cancelListener);
	}
	
	private View.OnClickListener searchListener = new View.OnClickListener(){
		public void onClick(View v){
			
			okListener.onClick(btnCompute);
		}
	};
	
	private View.OnClickListener cancelListener = new View.OnClickListener(){
		public void onClick(View v){
			
			dismiss();
		}
	};
	
	public void setDeparture(String dep){
		if(dep != null){
			departure = dep;
		}
	}
	
	public String getDeparture(){
		return txtDep.getText().toString();
	}
	
	public void setArrival(String arr){
		if(arr != null){
			arrival = arr;
		}
	}
	
	public String getArrival(){
		return txtArr.getText().toString();
	}
	
	public void setStopsList(String[] stops){
		this.stops = stops;
	}
	
	public void setOnClickListener(View.OnClickListener listener){
		okListener = listener;
	}
}

